#ifndef UI_ANIMATED_DIALOG_H
#define UI_ANIMATED_DIALOG_H

#include "ui_avatar_widget.h"
#include "thread_type_writer.h"

#include <QEventLoop>
#include <QDialog>
#include <QTimer>

namespace Ui {
class UiAnimatedDialog;
}

class UiAnimatedDialog : public QDialog
{
    Q_OBJECT

public:
    explicit UiAnimatedDialog(bool need_shake = false, \
                              int play_mode = ENUM_AVATAR_PLAY_ONCE, QWidget *parent = nullptr);
    ~UiAnimatedDialog();

    void SetAvatarType(const int& type);
    void SetAvatarTypeCustom(QMap<QString, QString> res, bool is_gif);
    void ShakeWidget();

    void Show(const QString& avatar_status, bool typewriter_mode = true, bool shake = false);
    int Exec(const QString& avatar_status, bool typewriter_mode = true, bool shake = false);

    inline void SetText(const QString& text){content_text_ = text;}

    static UiAnimatedDialog* CreateBasicDialog(bool need_shake, int play_mode, \
                                               const int& avatar_type, QWidget *parent);
    static UiAnimatedDialog* CreateCustomDialog(const QString& title, const QString& content, \
                                 const QMap<QString,QString> res, bool need_shake = false, \
                                 int play_mode = ENUM_AVATAR_PLAY_ONCE, bool is_gif = true, QWidget *parent = nullptr);
    static UiAnimatedDialog* CreateEvilDialog(const QString& title, const QString& content, \
                               const QMap<QString,QString> res, bool need_shake = false, \
                               int play_mode = ENUM_AVATAR_PLAY_ONCE, bool is_gif = true, QWidget *parent = nullptr);
    static UiAnimatedDialog* CreateAngelDialog(const QString& title, const QString& content, \
                                const QMap<QString,QString> res, bool need_shake = false, \
                                int play_mode = ENUM_AVATAR_PLAY_ONCE, bool is_gif = true, QWidget *parent = nullptr);
    static UiAnimatedDialog* CreateActorDialog(const QString& title, const QString& content, \
                                const QMap<QString,QString> res, bool need_shake = false, \
                                int play_mode = ENUM_AVATAR_PLAY_ONCE, bool is_gif = true, QWidget *parent = nullptr);

public:
    enum avatar_gif_play_times
    {
        ENUM_AVATAR_PLAY_ONCE = 0,
        ENUM_AVATAR_LOOP,
        ENUM_AVATAR_KEEP_GIF_SETTINGS
    };

private:
    Ui::UiAnimatedDialog *ui;

    QPoint current_pos_;
    QString content_text_;
    QTimer* shake_timer_;

    ThreadTypeWriter* type_thread_;

    bool need_shake_;
    int  position_;
    int  gif_play_mode_;

    const int const_max_times = 15;
    const int const_max_space = 5;

private slots:
    void slot_timeout();
    void slot_get_char(QChar);
    void slot_type_finished();
};

#endif // UI_ANIMATED_DIALOG_H
